﻿using Microsoft.Practices.Prism.Events;
using compshop.Modules.GeneralProductParameters.Presenter;
using compshop.Infrastructure;

namespace compshop.Modules.GeneralProductParameters
{
    public class EventsManager
    {
        private SubscriptionToken subscriptionToken;
        private IEventAggregator eventAggregator;

        public GeneralProductParametersPresenter Presenter { get; set; }
        public bool IsActive = false;

        public EventsManager(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
        }

        public void Subscribe()
        {
            ModuleActivityEvent moduleActivityEvent = eventAggregator.GetEvent<ModuleActivityEvent>();
            moduleActivityEvent.Subscribe(this.ActivityProcessor); 

            GeneralProductSelectedEvent selectedEvent = eventAggregator.GetEvent<GeneralProductSelectedEvent>();
            subscriptionToken = selectedEvent.Subscribe(Presenter.GeneralProductSelectedEventHandler, ThreadOption.UIThread, false, ActivityFilter);
        }

        private void ActivityProcessor(ModuleActivityState state)
        {
            if (state.ActivateModule == RegionNames.GeneralProductParametersContent)
            {
                if (!IsActive)
                {
                    // activation code place here
                    IsActive = true;
                }
            }
            else
            {
                if (state.DeactivateAll)
                {
                    if (IsActive)
                    {
                        // deactivation code place here
                        IsActive = false;
                    }
                }
            }

        }
        public bool ActivityFilter(int a)
        {
            return IsActive;
        }
    }   //public class EventsManager
}
